package com.ykj.controller;

import com.ykj.dto.Result;
import com.ykj.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/file")
public class PicController {

    @Autowired
    FileService fileService;

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) {
        log.info("save file name {}", file.getOriginalFilename());
        String filePath = fileService.saveFile(file);
        return Result.ok(filePath);
    }

    @GetMapping("/download")
    public Result downloadFile(HttpServletResponse response,
                                   @RequestParam(value = "fileName") String fileName) {

        Boolean result = fileService.downloadFile(response, fileName);
        return Result.ok(result);
    }

    @GetMapping("/list")
    public Result list() {
        return Result.ok(fileService.getFiles());
    }

}
